import { sqliteTable, index, text } from 'drizzle-orm/sqlite-core';

import { relations } from 'drizzle-orm';
import { auditSchema } from './audit';
import * as users from './users';

export const tableName = 'drivers';

export const route = 'drivers';

export const definition = {
  id: text('id').primaryKey(),
  name: text('title'),
  user_id: text('user_id')
    .notNull()
    .references(() => users.table.id),
  config: text('config', { mode: 'json' }).$type<any>(),
};

export const table = sqliteTable(
  tableName,
  {
    ...definition,
    ...auditSchema,
  },
  (table) => {
    return {
      driverUserIdIndex: index('driverUserIdIndex').on(table.user_id),
    };
  }
);

export const relation = relations(table, ({ one }) => ({
  user: one(users.table, {
    fields: [table.user_id],
    references: [users.table.id],
  }),
}));
